package org.example.bootvue.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.example.bootvue.entity.Category;
import org.example.bootvue.entity.Result;
import org.example.bootvue.mapper.CategoryMapper;
import org.example.bootvue.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YourName
 * @since 2025-07-29
 */
@RestController
@RequestMapping("/category")
@CrossOrigin(origins = "http://localhost:5173")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        categoryService.removeById(id);
        return Result.success();
    }
@PostMapping
    public Result add(@RequestBody @Validated Category category) {
    /*category.setCreateTime(LocalDateTime.now());
    category.setUpdateTime(LocalDateTime.now());
    categoryService.updateById(category);*/
    categoryService.add(category);
    return Result.success();
}
@GetMapping
public Result<List<Category>> getAll() {
    List<Category> list = categoryService.selist();
    return Result.success(list);
}
@GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
    return Result.success(categoryService.findbyid(id));
}
@PutMapping
    public Result update(@RequestBody @Validated(Category.Add.class) Category category) {
    categoryService.updateit(category);
    return Result.success();
    }
}
